/**
 * 自定义组件
 */
abstract class GameObject extends eui.Component implements eui.UIComponent {
	private isCreated: boolean = false;
	//是否自动适应分辨率
	public autoResize: boolean = true;
	//组件长宽与舞台长宽的比例
	public heightPercent: number = 0;
	public widthPercent: number = 0;
	//组件在舞台中的位置
	public xPercent: number = -1;
	public yPercent: number = -1;
	//是否将组件锚点移至中心
	public setAnchorToCenter: boolean = false;
	public background: egret.DisplayObject;
	public displayGrp: egret.DisplayObject;

	type = "";
	//构造函数
	public constructor() {
		super();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.init, this);
		this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeFromStage, this);

	}
	//添加到舞台后执行
	protected async init() {
		!this.isCreated && await this.firstCreate();
		//console.log("added_to_stage", this);
		this.isCreated = true;
		//舞台分辨率变化后自动设置分辨率和位置
		this.stage && this.stage.addEventListener(egret.Event.RESIZE, this.resizeAndPlace, this);
		//将组件锚点移至中心
		if (this.setAnchorToCenter) {
			this.anchorOffsetX = this.width / 2;
			this.anchorOffsetY = this.height / 2;
		}
		this.addToStage();
		//分辨率设置
		await this.resizeAndPlace();
		this.afterResize();
	}
	protected removeFromStage() {
		this.stage.removeEventListener(egret.Event.RESIZE, this.resizeAndPlace, this);
		this.onRemove();
	}
	//组件被移除后执行
	protected abstract onRemove();
	//第一次初始化
	protected async abstract firstCreate();
	//添加到舞台
	protected abstract addToStage();
	//从父容器移除
	public removeFromParent() {
		this.parent && this.parent.removeChild(this);
	}

	//更换容器
	public changeContainer(dc: egret.DisplayObjectContainer) {
		let globalPos = this.parent.localToGlobal(this.x, this.y);
		let localPos = dc.globalToLocal(globalPos.x, globalPos.y);
		this.removeFromParent();
		dc.addChild(this);
		this.x = localPos.x;
		this.y = localPos.y;
	}
	/**
	 * 分辨率适配
	 */
	public async resizeAndPlace() {
		if (!this.autoResize) {
			return;
		}
		if (!this.stage) {
			return;
		}
		let widthP = this.widthPercent;
		let heightP = this.heightPercent;
		let xP = this.xPercent;
		let yP = this.yPercent;
		let stageW = this.stage.stageWidth;
		let stageH = this.stage.stageHeight;
		//组件大小调整
		if (widthP != 0) {
			this.width = stageW * widthP;
		}
		if (heightP != 0) {
			this.height = stageH * heightP;
		}
		else if (widthP != 0) {
			this.height = this.height * widthP;
		}
		//组件在舞台位置调整
		if (xP != -1) {
			this.x = stageW * xP;
		}
		if (yP != -1) {
			this.y = stageH * yP;
		}
		//背景图适配
		if (this.background) {
			let bgWH = this.background.width / this.background.height;
			let stageWH = window.innerWidth / window.innerHeight;
			if (stageWH < 1) {
				stageWH = window.innerHeight / window.innerWidth;
			}
			if (bgWH > stageWH) {
				this.childResize({ heightPercent: 1 }, this.background);

			}
			else {
				this.childResize({ widthPercent: 1 }, this.background);
			}
			this.childResize({ xPercent: 0.5, yPercent: 0.5 }, this.background);
		}
		//显示区域适配
		if (this.displayGrp) {
			let gWH = this.displayGrp.width / this.displayGrp.height;
			let stageWH = window.innerWidth / window.innerHeight;
			if (stageWH < 1) {
				stageWH = window.innerHeight / window.innerWidth;
			}
			if (gWH > stageWH) {
				this.childResize({ widthPercent: 0.95 }, this.displayGrp);
			}
			else {
				this.childResize({ heightPercent: 0.95 }, this.displayGrp);
			}
			this.childResize({ xPercent: 0.5, yPercent: 0.5 }, this.displayGrp);
		}

		//子对象分辨率适配
		this.childrenResize();
	}
	//调整完分辨率后执行
	protected afterResize() {

	}
	//子对象分辨率适配
	protected childrenResize() {
	}
	//子对象分辨率适配
	protected childResize({ widthPercent = 0, heightPercent = 0, xPercent = -1,
		yPercent = -1, useCenterAnchor = true,
		anchorX = 0, anchorY = 0 }, displayObject: egret.DisplayObject) {
		if (!displayObject) {
			return;
		}
		if (!this.stage) {
			return;
		}
		let stageW = this.width;
		let stageH = this.height;
		//组件大小调整
		if (widthPercent != 0) {
			displayObject.scaleX = (stageW * widthPercent) / displayObject.width;
		}
		if (heightPercent != 0) {
			displayObject.scaleY = (stageH * heightPercent) / displayObject.height;
			if (widthPercent == 0) {
				displayObject.scaleX = displayObject.scaleY;
			}
		}
		else if (widthPercent != 0) {
			displayObject.scaleY = displayObject.scaleX;
		}
		//组件在舞台位置调整
		if (xPercent != -1) {
			displayObject.x = stageW * xPercent - (useCenterAnchor ? 0 :
				(displayObject.width * displayObject.scaleX / 2));
		}
		if (yPercent != -1) {
			displayObject.y = stageH * yPercent - (useCenterAnchor ? 0 :
				(displayObject.height * displayObject.scaleY / 2));
		}
		//将锚点设置在中心
		if (useCenterAnchor) {
			displayObject.anchorOffsetX = displayObject.width / 2;
			displayObject.anchorOffsetY = displayObject.height / 2;
		}
		(anchorX != 0) && (displayObject.anchorOffsetX = anchorX);
		(anchorY != 0) && (displayObject.anchorOffsetY = anchorY);

	}
}